package com.star.vrserver.controller;

import com.star.vrserver.api.R;
import com.star.vrserver.common.CommonTools;
import com.star.vrserver.entity.Manufacture;
import com.star.vrserver.entity.query.ManufactureQuery;
import com.star.vrserver.entity.vo.PageVO;
import com.star.vrserver.service.ManufactureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author star
 * @since 2023-03-09
 */
@Api(tags = "疫苗生产厂家信息管理Api")
@RestController
@RequestMapping("/manufacture")
public class ManufactureController {
    @Autowired
    private ManufactureService manufactureService;

    //获取所有生产厂商信息
    @ApiOperation(value = "获取所有生产厂商信息")
    @GetMapping("getAllManufactures")
    public R getManufactures() {
        return R.ok().data("ManufactureLists", manufactureService.list());
    }

    //根据id查询生产厂商信息
    @ApiOperation(value = "根据id查询生产厂商信息")
    @GetMapping("getManufacture")
    public R getManufacture(@RequestParam String id) {
        return R.ok().data("manufactureInfo", manufactureService.getById(id));
    }

    //根据id删除生产厂商
    @ApiOperation(value = "根据id删除生产厂商")
    @DeleteMapping("deleteManufacture/{id}")
    public R deleteManufacture(@PathVariable String id) {
        //是否删除成功：逻辑删除
        boolean flag = manufactureService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    //添加生产厂商
    @ApiOperation(value = "添加生产厂商")
    @PostMapping("saveManufacture")
    public R saveManufacture(@RequestBody Manufacture manufacture) {
        boolean flag = manufactureService.save(manufacture);
        return flag ? R.ok() : R.error();
    }

    //分页查询生产厂商  current 当前页  limit 每页记录数
    @ApiOperation(value = "分页展示生产厂商列表")
    @GetMapping("pageManufacture/{current}/{limit}")
    public R pageListManufacture(@PathVariable long current,
                             @PathVariable long limit) {
        //调用通用工具类的数组分页方法进行分页（不加条件）
        PageVO<Manufacture> pageVO = CommonTools.pageList(manufactureService,current,limit);
        long total = pageVO.getTotal();
        List<Manufacture> records = pageVO.getRecords();
        return R.ok().data("total", total).data("records", records);
    }

    //条件查询带分页的方法
    @ApiOperation(value = "分页展示生产厂商列表(带条件)")
    @PostMapping("pageManufactureCondition/{current}/{limit}")
    public R pageListManufacture(@PathVariable long current, @PathVariable long limit,
                             @RequestBody(required = false) ManufactureQuery manufactureQuery) {
        return R.ok();
    }
    //修改生产厂商信息
    @ApiOperation(value = "修改生产厂商信息")
    @PostMapping("updateManufacture")
    public R updateManufacture(@RequestBody Manufacture manufacture) {
        boolean flag = manufactureService.updateById(manufacture);
        return flag ? R.ok() : R.error();
    }

}
